package com.jamaskii.ecdict;

import com.alibaba.fastjson.JSONObject;
import com.jamaskii.dictation.HomeActivity;
import com.jamaskii.utils.IOUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.Map;

public class WordBookStream {
    public String path;
    public String error;

    public ArrayList<WordBook> wordBooks=new ArrayList<>();

    public WordBookStream(String wordBooksPath){
        path=wordBooksPath;
        checkPath();
    }

    public boolean checkPath(){
        if(path==null){
            error="PathNullException";
            return false;
        }else if(path.equals("")){
            error="PathEmptyException";
            return false;
        }else{
            error=null;
            return true;
        }
    }

    public void loadWordBooks(){
        wordBooks.clear();
        File objFile = new File(path);
        File[] files=objFile.listFiles();
        if(files==null){
            return;
        }
        for (File file:files
             ) {
            wordBooks.add(WordBook.loads(file.getAbsolutePath().replace("\\", "/")));
        }
    }

    public boolean addBook(WordBook wordBook){
        if(!checkExits(wordBook.name)){
            wordBooks.add(wordBook);
            if(wordBook.save(path)){
                error=null;
                return true;
            }else{
                error=wordBook.error;
                return false;
            }
        }else{
            error="WordBookAlreadyExitsException";
            return false;
        }
    }

    public boolean checkExits(String name){
        for (WordBook book:wordBooks
        ) {
            if(book.name.equals(name)){
                return true;
            }
        }
        return false;
    }

    public boolean removeBook(String bookName){
        try{
            int index=-1;
            int i=0;
            while (i<wordBooks.size()){
                if(wordBooks.get(i).name.equals(bookName)){
                    index=i;
                    break;
                }
                i++;
            }
            if(index==-1){
                error="BookNotFoundException";
                return false;
            }
            else{
                return removeBook(index);
            }
        }catch (Exception e){
            error=e.toString();
            return false;
        }

    }

    public boolean removeBook(int index){
        if(index>-1 && index<wordBooks.size()){
            File file=new File(path+"/"+wordBooks.get(index).name+".wb");
            wordBooks.remove(index);
            if(file.delete()){
                error=null;
                return true;
            }else{
                error="DeleteFileException";
                return false;
            }
        }else{
            error="IndexOutOfBound";
            return false;
        }
    }

    public boolean renameBook(int index,String name){
        if(index<-1 || index>wordBooks.size()-1){
            error="OutOfBoundException";
            return false;
        }else if(checkExits(name)){
            error="BookNameDuplicateException";
            return false;
        }else{
            try{
                File oldFile=new File(getBookPath(index));
                if(!oldFile.delete()){
                    error="DeleteOldFileException";
                    return false;
                }
                wordBooks.get(index).name=name;
                if(!wordBooks.get(index).save(path)){
                    error=wordBooks.get(index).error;
                    return false;
                }
                error=null;
                return true;
            }catch (Exception e){
                error=e.toString();
                return false;
            }
        }
    }

    public String getBookPath(int index){
        return path+wordBooks.get(index).name+".wb";
    }

}
